﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Wassrile.Dialogs.Pictogram
{
    public partial class PictogramSet : UserControl
    {
        public PictogramSet()
        {
            InitializeComponent();
        }

        private void PictogramSet_Load(object sender, EventArgs e)
        {
            palletScroll.SmallChange = PictogramPallet.pictHeight;
        }

        void pallet_OnBufferUpdated(object sender, EventArgs e)
        {
            ScrollUpdate();
        }

        public String[] Pictograms
        {
            get { return pallet.Pictograms; }
            set
            {
                pallet.Pictograms = value;
                ScrollUpdate();
            }
        }

        internal void ScrollUpdate()
        {
            int pRH = pallet.ActualHeight;
            int pH = pallet.Height;
            palletScroll.LargeChange = pH;
            if (pRH == 0 || pH > pRH)
            {
                palletScroll.Maximum = 0;
                palletScroll.Value = 0;
            }
            else
            {
                for (int i = 0; i < 100; i++)
                    palletScroll.Maximum = pRH - pH;
                if (palletScroll.Value > palletScroll.Maximum)
                    palletScroll.Value = palletScroll.Maximum;
            }
        }

        private void palletScroll_Scroll(object sender, ScrollEventArgs e)
        {
            palletScroll_ValueChanged(sender, e);
        }

        private void palletScroll_ValueChanged(object sender, EventArgs e)
        {
            pallet.VerticalPosition = palletScroll.Value;
        }

        public class StringEventArgs : EventArgs
        {
            public StringEventArgs(string value)
            {
                code = value;
            }

            public string code;
        }

        public delegate void ClickPallet(object sender, StringEventArgs e);
        public event ClickPallet OnClickPallet;

        private void pallet_MouseClick(object sender, MouseEventArgs e)
        {
            if (Pictograms != null && OnClickPallet != null)
            {
                if ((int)Math.Ceiling((double)e.X / PictogramPallet.pictWidth) > pallet.GetWidthCapacity()) return;
                int index = (int)Math.Floor((double)e.X / PictogramPallet.pictWidth);
                int yLoc = (int)Math.Floor((double)(e.Y + palletScroll.Value) / PictogramPallet.pictHeight);
                index += pallet.GetWidthCapacity() * yLoc;
                if (index >= 0 && Pictograms.Length > index)
                    OnClickPallet.Invoke(this, new StringEventArgs(Pictograms[index]));
            }
        }

        private void PictogramSet_Resize(object sender, EventArgs e)
        {
            ScrollUpdate();
        }
    }
}
